package oa.struts.action;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import oa.domain.page.PageBean;
import oa.domain.workflow.Approve;
import oa.domain.workflow.Form;
import oa.domain.workflow.FormTemplate;
import oa.domain.workflow.TaskView;
import oa.service.FormService;
import oa.service.FormTemplateService;
import oa.service.WorkFlowService;
import oa.util.HQLHelper;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("workFlowAction")
public class WorkFlowAction extends ActionSupport{

	private File resource;
	private Long fid;
	private Long ftid;
	private String processKey;
	private String isapprove;
	private Long taskId;
	private String applicator;
	private InputStream inputStream;
	private String comment;
	
	public String getApplicator() {
		return applicator;
	}
	public void setApplicator(String applicator) {
		this.applicator = applicator;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public Long getFid() {
		return fid;
	}
	public void setFid(Long fid) {
		this.fid = fid;
	}
	public Long getTaskId() {
		return taskId;
	}
	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}
	public File getResource() {
		return resource;
	}
	public void setResource(File resource) {
		this.resource = resource;
	}
	public Long getFtid() {
		return ftid;
	}
	public void setFtid(Long ftid) {
		this.ftid = ftid;
	}
	public String getProcessKey() {
		return processKey;
	}
	public void setProcessKey(String processKey) {
		this.processKey = processKey;
	}

	public String getIsapprove() {
		return isapprove;
	}
	public void setIsapprove(String isapprove) {
		this.isapprove = isapprove;
	}

	@Resource(name="formTemplateService")
	private FormTemplateService formTemplateService;
	
	@Resource(name="formService")
	private FormService formService;
	
	@Resource(name="workFlowService")
	private WorkFlowService workFlowService;
	/**
	 * 跳转到表单模板的列表页面
	 * @return
	 */
	public String showFormTemplate(){
		Collection<FormTemplate> formTemplateList = this.formTemplateService.getAllFormtemplate();
		ActionContext.getContext().put("formTemplateList", formTemplateList);
		return "formTemplateList";
	}
	/**
	 * 跳转到提交申请的页面
	 * @return
	 */
	public String submitUI(){
		return "submitUI";
	}
	
	/**
	 * 提交申请
	 * @return
	 */
	public String submit(){
        this.workFlowService.submit(resource, ftid, processKey);
        ActionContext.getContext().getValueStack().push("申请提交成功，等待处理......");
		return "finish";
	}
	

	/**
	 * 我的待审批的列表
	 * @return
	 */
	public String myTaskList()
	{
		Collection<TaskView> myTaskList = this.workFlowService.myTaskList2();
		ActionContext.getContext().put("taskViewList", myTaskList);
		return "myTaskList";
	}
	/**
	 * 跳转到审批页面
	 * @return
	 */
	public String approveUI(){
//		ActionContext.getContext().put("pdList", pdList);
		return "approveUI";
	}
	public String approve()
	{
	    this.workFlowService.approve(fid, taskId,isapprove,comment);
	    ActionContext.getContext().getValueStack().push("审批已经处理，等待下一步处理......");
		return "finish";
	}
	
	/**
	 *我的申请查询列表
	 * @return
	 */
	public String myApplicationList()
	{
		Collection<Form> myAppList = this.formService.myApplicationList();
		ActionContext.getContext().put("myAppList", myAppList);
		
		return "myApplicationList";
	}
	
	/**
	 * 查看申请信息
	 * @return
	 */
	public String showForm()
	{
		Form form = this.formService.getFormById(fid);
		ActionContext.getContext().getValueStack().push(form);
		return "showForm";
	}
	
	/**
	 * 下载申请信息
	 */
	
	public String downLoad() throws Exception
	{
		this.inputStream = this.formService.download(fid);
		return "download";
	}
	/**
	 * 查看流转记录
	 */
	public String historyApprovedList()
	{
		List<Approve> approveList = this.formService.getApprovesById(fid);
		ActionContext.getContext().put("approveList", approveList);
		return "historyApprovedList";
	}
}
 